package sctu.edu.java.year2016.team08.project;

/**
 * Created by 松红 on 2016/6/29.
 */

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Search extends JFrame {
    public Search() {
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JTextArea area = new JTextArea("请输入你是所查询的问题:" + "\n");
        contentPane.add(area, BorderLayout.NORTH);
        JTextField field = new JTextField();
        contentPane.add(field, BorderLayout.SOUTH);
        setVisible(true);
        setTitle("IT运维答案搜索系统");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        field.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException e2) {
                    e2.printStackTrace();
                }
                try {
                    String url = "jdbc:mysql://localhost:3306/lsongh";
                    String user = "root";
                    String Password = "";
                    Connection conn = DriverManager.getConnection(url, user, Password);
                    String string = null;
                    string = field.getText();
                    String sql3 = "select * from itrun where question='" + string + "'  ";
                    PreparedStatement ps3 = conn.prepareStatement(sql3);
                    ResultSet rs = ps3.executeQuery(sql3);
                    int id;
                    String question, answer;
                    while (rs.next()) {
                        id = rs.getInt(1);
                        question = rs.getString(2);
                        answer = rs.getString("answer");
                        field.setText(" ");
                        area.append(id + "\t" + question + "\t" + answer);
                    }
                    conn.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }

            }

        });

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Search();
    }

}
